package com.zjitc.controller;

import com.zjitc.beans.MessageBean;
import com.zjitc.pojo.Category;
import com.zjitc.pojo.Product;
import com.zjitc.service.CategoryService;
import com.zjitc.service.ProductService;
import com.zjitc.utils.FileCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;

    private static final String sufPath = ".jpg";

    @RequestMapping("product")
    public ModelAndView Toproduct(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        if (!checkAdmin(session)) {
            modelAndView.setViewName("login");
        } else {
            modelAndView.setViewName("product/product");
            List<Product> productList = productService.findall();
            int count = productService.findcount();
            List<Category> categories = categoryService.adminfindAll();
            modelAndView.addObject("category", categories);
            modelAndView.addObject("list", productList);
            modelAndView.addObject("size", count);
        }
        return modelAndView;
    }

    @RequestMapping("/changeCategory")
    public ModelAndView changeCategory(HttpSession session,String cid){
        ModelAndView modelAndView = new ModelAndView();
        if (!checkAdmin(session)) {
            modelAndView.setViewName("login");
        } else {
            modelAndView.setViewName("product/product");
            List<Product> productList = productService.changeCategory(cid);
            int count = productService.findcount();
            List<Category> categories = categoryService.adminfindAll();
            modelAndView.addObject("cid",cid);
            modelAndView.addObject("category", categories);
            modelAndView.addObject("list", productList);
            modelAndView.addObject("size", count);
        }
        return modelAndView;
    }

    @RequestMapping("toEditProduct")
    public ModelAndView toEditProduct(String pid, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        if (!checkAdmin(session)) {
            modelAndView.setViewName("login");
        } else {
            modelAndView.setViewName("product/editProduct");
            Product product = productService.findbyid(pid);
            modelAndView.addObject("type","product");
            modelAndView.addObject("product", product);
        }
        return modelAndView;
    }

    @RequestMapping("/editProduct")
    @ResponseBody
    public Object editProduct(Product product, @RequestParam(value = "image", required = false) String image, HttpServletRequest request) {
        if (image == "") {
        } else {
            String img = savefile(product.getPid(), request, image);
            product.setImgs(img);
        }
        int i = productService.updateProduct(product);
        return i == 1;
    }


    @RequestMapping("toAddProduct")
    public ModelAndView toAddProduct() {
        ModelAndView modelAndView = new ModelAndView("product/addProduct");
        List<Category> list = categoryService.adminfindAll();
        modelAndView.addObject("list", list);
        return modelAndView;
    }

    @RequestMapping("AddProduct")
    @ResponseBody
    public Object AddProduct(Product product, @RequestParam(value = "image", required = true) String image, HttpServletRequest request) {
        String savefile = savefile(product.getPid(), request, image);
        product.setImgs(savefile);
        int i = productService.addproduct(product);
        return i==1;
    }

    @RequestMapping("toEditStock")
    @ResponseBody
    public ModelAndView toEditStock(String pid, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        if (!checkAdmin(session)) {
            modelAndView.setViewName("login");
        } else {
            modelAndView.setViewName("product/editProduct");
            Product product = productService.findbyid(pid);
            List<Category> categories = categoryService.adminfindAll();
            modelAndView.addObject("list",categories);
            modelAndView.addObject("type","stock");
            modelAndView.addObject("product", product);
        }
        return modelAndView;
    }

    @RequestMapping("/editStock")
    @ResponseBody
    public Object EditStock(Product p) {
        Product product = productService.findbyid(p.getPid());
        product.setStock(p.getStock());
        int i = productService.updateProduct(product);
        return i==1;
    }

    @RequestMapping("findProduct")
    public ModelAndView findProduct(@RequestParam("name") String name, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        if (!checkAdmin(session)) {
            modelAndView.setViewName("login");
        } else {
            List<Product> products = productService.FuzzyProduct(name);
            modelAndView.setViewName("product/product");
            modelAndView.addObject("size", products.size());
            modelAndView.addObject("list", products);
        }
        return modelAndView;
    }

    @RequestMapping("/updateState")
    @ResponseBody
    public String updateState(String pid, HttpSession session) {
        if (!checkAdmin(session)) {
            return "-1";
        } else {
            return productService.updateState(pid);
        }
    }

    @RequestMapping("delProduct")
    @ResponseBody
    public MessageBean delProduct(String pid) {
        productService.deleteByPid(pid);
        return new MessageBean(true, "商品删除成功");
    }
//
    private String savefile(String pid, HttpServletRequest request, String file) {
        String ImgPath = "";
        String imageName = pid;
        String prePath = request.getSession().getServletContext().getRealPath("").replace("shopAdmin", "shopClient");
        String dirPath = prePath + "static\\img\\products";
        FileCheck.isMkDir(dirPath);
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] image;
        try {
            image = decoder.decodeBuffer(file);
            ImgPath = "static/img/products/" + imageName + sufPath;
            String path = dirPath + "\\" + imageName + sufPath;
            FileCheck.deleteFile(path);
            OutputStream outputStream = new FileOutputStream(new File(path));
            outputStream.write(image);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ImgPath;
    }
//

}
